const { Sequelize } = require('sequelize')
const logger = require('./logger')
const { dbName, host, port, username, password } = require('../config').database

const sequelize = new Sequelize(dbName, username, password, {
  dialect: 'mysql',
  host,
  port,
  logging: (msg) => logger.info(msg), // 记录日志
  timezone: '+08:00',
  define: {
    // create_time && update_time
    timestamps: true,
    // delete_time
    paranoid: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    // 把驼峰命名转换为下划线
    underscored: true,
    freezeTableName: true,
    scopes: {
      bh: {
        attributes: {
          exclude: ['updated_at', 'deleted_at', 'created_at'],
        },
      },
    },
  },
})

// 自动同步所有模型
// sequelize.sync({ force: true })

module.exports = {
  sequelize,
}
